package hexui;

import javax.swing.JOptionPane;

import hexgame.GameState;
import hexgame.HexMove;
import hexgame.Player;

public class LocalPlayer extends Player {

	// Store the GameWindow here
	GameWindow window;
	boolean hasMove = false;
	HexMove move = null;

	public LocalPlayer(GameState gameState) {
		this.window = new GameWindow(gameState, this);
	}

	@Override
	public boolean isConnected() {
		return true;
	}

	@Override
	public HexMove getMove() {
		window.setLocalTurn(true);
		while (!hasMove) {
			try {
				// Prevents us from making the game unplayable on
				// single-core computers
				Thread.sleep(50);
			} catch (InterruptedException e) {

			}
		}
		hasMove = false;
		window.setLocalTurn(false);
		return move;

	}

	@Override
	public void showMove(HexMove move) {
		window.showMove(move);
	}

	@Override
	public void gameOver(boolean didWin) {
		if (didWin) {
			JOptionPane.showMessageDialog(null, "You have won!");
			window.setVisible(false);
		} else {
			JOptionPane.showMessageDialog(null, "Sorry, you have lost...");
			window.setVisible(false);
		}
	}

	@Override
	public void sendErrorMessage(String message) {
		JOptionPane.showMessageDialog(null, message);
		window.setVisible(false);
	}

	public void setCurrentMove(HexMove move) {
		this.move = move;
		this.hasMove = true;
	}
}
